<?php
	require_once ("../../config.php");
	
	# Verify what button was clicked.
	$btaction = $_POST["submit_loginedit"];
	if ($btaction == "Voltar"){
		header("Location: /itcontrol/com/main/login/login_list.php");
	}else{
		
		# Array with the values
		$values[0] = $_POST["input_id"];
		$values[1] = $_POST["input_name"];
		$values[2] = $_POST["input_lastname"];
		$values[3] = $_POST["input_login"];
		$values[4] = $_POST["input_email"];
		
		# Array with fields
		$fields[0] = "id";
		$fields[1] = "name";
		$fields[2] = "lastname";
		$fields[3] = "login";
		$fields[4] = "email";
		
		# Array with Alias from fields
		$namefields[0] = "ID";
		$namefields[1] = "Nome";
		$namefields[2] = "Sobrenome";
		$namefields[3] = "Login";
		$namefields[4] = "Email";
		
		# Mount array where which contains the id
		$where[0] = $fields[0] ."='" .$values[0]. "'";
		
		# Connect with DB
		$dbconnection = new connectDB();
		
		# Check what's the action
		switch ($btaction){
			# Case the pressed button is Atualizar, then
			case "Atualizar":
				# Run the select command with the previous info
				$query = mysql_query($dbconnection->selectDB("tb_login", "login", "update", $fields, $namefields, $where));
				$result = mysql_fetch_array($query);
				$updatefields;			
				$count = 0;
				
				# Identifies which fields was changed
				for ($i = 0; $i < count($values); $i++) {
					if ($values[$i] <> $result[$i]){
						$updatefields[$count] = $fields[$i]. "='" .$values[$i]. "'";
						$count = $count + 1;
					}
				}
				
				
				# Check if there is one or more empty fields. Having a empty field then change the value of $error and redirect to the add login page with the 
				# previous inserted data
				if (empty($values[0])){
					$error = TRUE;
				}
				if (empty($values[1])){
					$error = TRUE;
				}
				if (empty($values[2])){
					$error = TRUE;
				}
				if (empty($values[3])){
					$error = TRUE;
				}
				if (empty($values[4])){
					$error = TRUE;
				}
				
				# Check if the array with the changed values are empty or not
				if ($error == FALSE) {
					# If doesn't are empty, runs update
					$query = $dbconnection->updateDB("tb_login", $updatefields, $where);;
				}else{
					# Else, shows error
					$query = "fail";
				}
					
				# Mount the parameter of redirection
				$query = "login-upd-" .$query;
				
				# Disconnect with DB
				$dbconnection->disconnectDB();
	
				# Redirect to main page and show the result (positive or negative)
				header("Location: /itcontrol/com/redirect/redirect.php?action=".$query);
				break;
			case "Deletar":
				# Check if exist registry
				$check = $dbconnection->checkDB("tb_login", "login", "delete", $fields, $namefields, $where);
				# If exist, do the deletion (This test it's obvious and retarded, but i'll do.
				if ($check <> 0) {
					# Delete registry
					$query = $dbconnection->deleteDB("tb_login", $where);
					
					# Mount the parameter of redirection
					$query = "login-del-" .$query;
					
					# Disconnect with DB
					$dbconnection->disconnectDB();
					
					# Redirect to main page and show the result (positive or negative)
					header("Location: /itcontrol/com/redirect/redirect.php?action=".$query);			
				}
				break;
		}
	}
	
?>